package com.gamesnet.widget;

import java.util.LinkedList;

import com.gamesnet.ihm.R;

import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * Action bar widget
 * 
 * @author marin.heuripeau@gmail.com
 *
 */

public class ActionBar extends RelativeLayout implements OnClickListener {

	private LayoutInflater mInflater;
	private RelativeLayout mBarView;
	private ImageView mLogoView;
	private View mBackIndicator;
	private LinearLayout mActionsView;
	private ImageButton mHomeBtn;
	private RelativeLayout mHomeLayout;
	private ProgressBar mProgress;
	private TextView titreBar;

	public ActionBar(Context context, AttributeSet attrs) {
		super(context, attrs);

		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		mBarView = (RelativeLayout) mInflater.inflate(R.layout.actionbar, null);
		addView(mBarView);

		mLogoView = (ImageView) mBarView.findViewById(R.id.actionbar_home_logo);
		mHomeLayout = (RelativeLayout) mBarView.findViewById(R.id.actionbar_home_bg);
		mHomeBtn = (ImageButton) mBarView.findViewById(R.id.actionbar_home_btn);
		mBackIndicator = mBarView.findViewById(R.id.actionbar_home_is_back);       
		mActionsView = (LinearLayout) mBarView.findViewById(R.id.actionbar_actions);
		
		titreBar = (TextView) mBarView.findViewById(R.id.titrePage);
	}

	/**
	 * Initialise le bouton Home et le place a gauche
	 * 
	 * @param action
	 */
	public void setHomeAction(Action action) {
		mHomeBtn.setOnClickListener(this);
		mHomeBtn.setTag(action);
		mHomeBtn.setImageResource(action.getDrawable());
		mHomeLayout.setVisibility(View.VISIBLE);
	}

	public void clearHomeAction() {
		mHomeLayout.setVisibility(View.GONE);
	}


	public void setHomeLogo(int resId) {
		mLogoView.setImageResource(resId);
		mLogoView.setVisibility(View.VISIBLE);
		mHomeLayout.setVisibility(View.GONE);
	}

	
	public void setDisplayHomeAsUpEnabled(boolean show) {
		mBackIndicator.setVisibility(show? View.VISIBLE : View.GONE);
	}


	
	public void setProgressBarVisibility(int visibility) {
		mProgress.setVisibility(visibility);
	}

	
	public int getProgressBarVisibility() {
		return mProgress.getVisibility();
	}

	public void onClick(View view) {
		final Object tag = view.getTag();
		if (tag instanceof Action) {
			final Action action = (Action) tag;
			action.performAction(view);
		}
	}

	
	public void addActions(ActionList actionList) {
		int actions = actionList.size();
		for (int i = 0; i < actions; i++) {
			addAction(actionList.get(i));
		}
	}

	
	public void addAction(Action action) {
		final int index = mActionsView.getChildCount();
		addAction(action, index);
	}

	
	public void addAction(Action action, int index) {
		mActionsView.addView(inflateAction(action), index);
	}

	public void removeAllActions() {
		mActionsView.removeAllViews();
	}

	
	public void removeActionAt(int index) {
		mActionsView.removeViewAt(index);
	}


	public void removeAction(Action action) {
		int childCount = mActionsView.getChildCount();
		for (int i = 0; i < childCount; i++) {
			View view = mActionsView.getChildAt(i);
			if (view != null) {
				final Object tag = view.getTag();
				if (tag instanceof Action && tag.equals(action)) {
					mActionsView.removeView(view);
				}
			}
		}
	}

	
	public int getActionCount() {
		return mActionsView.getChildCount();
	}

	
	private View inflateAction(Action action) {
		View view = mInflater.inflate(R.layout.actionbar_item, mActionsView, false);

		ImageButton labelView =
				(ImageButton) view.findViewById(R.id.actionbar_item);
		labelView.setImageResource(action.getDrawable());

		view.setTag(action);
		view.setOnClickListener(this);
		return view;
	}

	@SuppressWarnings("serial")
	public static class ActionList extends LinkedList<Action> {
	}

	
	public interface Action {
		public int getDrawable();
		public void performAction(View view);
	}

	public static abstract class AbstractAction implements Action {
		final private int mDrawable;

		public AbstractAction(int drawable) {
			mDrawable = drawable;
		}


		public int getDrawable() {
			return mDrawable;
		}
	}

	public static class IntentAction extends AbstractAction {
		private Context mContext;
		private Intent mIntent;

		public IntentAction(Context context, Intent intent, int drawable) {
			super(drawable);
			mContext = context;
			mIntent = intent;
		}


		public void performAction(View view) {
			try {
				mContext.startActivity(mIntent); 
			} catch (ActivityNotFoundException e) {
				Toast.makeText(mContext,
						mContext.getText(R.string.actionbar_activity_not_found),
						Toast.LENGTH_SHORT).show();
			}
		}
	}
	
	public void setTitre(String titre){
		titreBar.setText(titre);
	}


}
